package cn.sfrog.project.biz.admin.service.impl;

import cn.sfrog.common.utils.SecurityUtils;
import cn.sfrog.project.biz.admin.domain.BizBanner;
import cn.sfrog.project.biz.admin.domain.vo.BizBannerFormVO;
import cn.sfrog.project.biz.admin.domain.vo.BizBannerQueryVO;
import cn.sfrog.project.biz.admin.domain.vo.BizBannerVO;
import cn.sfrog.project.biz.admin.mapper.BizBannerMapper;
import cn.sfrog.project.biz.admin.service.IBizBannerService;
import cn.sfrog.project.biz.web.vo.index.AppBannerResultVO;
import cn.sfrog.system.domain.vo.BizStatusChangeVO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 广告轮播图Service业务层处理
 */
@Service
public class BizBannerServiceImpl extends ServiceImpl<BizBannerMapper, BizBanner> implements IBizBannerService {
    @Resource
    private BizBannerMapper bizBannerMapper;

    /**
     * 查询广告轮播图
     *
     * @param id 广告轮播图主键
     * @return 广告轮播图
     */
    @Override
    public BizBannerVO selectBizBannerById(Long id) {
        return bizBannerMapper.selectBizBannerById(id);
    }

    /**
     * 查询广告轮播图列表
     *
     * @param query 广告轮播图
     * @return 广告轮播图
     */
    @Override
    public List<BizBannerVO> selectBizBannerList(BizBannerQueryVO query) {
        return bizBannerMapper.selectBizBannerList(query);
    }

    /**
     * 新增广告轮播图
     *
     * @param form 广告轮播图
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean insertBizBanner(BizBannerFormVO form) {
        BizBanner bizBanner = new BizBanner();
        BeanUtils.copyProperties(form, bizBanner);
        bizBanner.setCreateTime(new Date());
        bizBanner.setCreateBy(SecurityUtils.getUsername());
        bizBanner.setUpdateTime(new Date());
        bizBanner.setUpdateBy(SecurityUtils.getUsername());
        return bizBanner.insert();
    }

    /**
     * 修改广告轮播图
     *
     * @param form 广告轮播图
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean updateBizBanner(BizBannerFormVO form) {
        BizBanner bizBanner = new BizBanner();
        BeanUtils.copyProperties(form, bizBanner);
        bizBanner.setUpdateTime(new Date());
        bizBanner.setUpdateBy(SecurityUtils.getUsername());
        return bizBanner.updateById();
    }

    /**
     * 批量删除广告轮播图
     *
     * @param ids 需要删除的广告轮播图主键
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean deleteBizBannerByIds(Long[] ids) {
            return BizBanner.DAO.update(Wrappers.lambdaUpdate(BizBanner.class).set(BizBanner::getDelFlag,"1").in(BizBanner::getId, (Object[]) ids));
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean changeStatus(BizStatusChangeVO vo) {
        return BizBanner.DAO.update(Wrappers.lambdaUpdate(BizBanner.class).in(BizBanner::getId, (Object[]) vo.getIds()).set(BizBanner::getStatus, vo.getStatus()));
    }

    @Override
    public List<AppBannerResultVO> selectAppIndexBannerList() {
        return bizBannerMapper.selectAppIndexBannerList();
    }
}
